package net.sevear.chaos.utils
{
	public class Queue
	{
		private var data:Object;  
		private var maxSize:int;  
		private var size:int;  
		private var front:int; //允许删除的一端  
		private var rear:int;   //允许插入的一端  
		
		/**
		 *
		 * @param maxSize - 队列最大长度
		 */
		public function Queue(maxSize:int) {  
			this.maxSize = maxSize;  
			this.data = {};  
			size= 0;  
			rear = 0;  
			front =0;  
		}  
		
		public function isEmpty():Boolean {  
			return size==0;  
		}  
		
		public function isFull():Boolean {  
			return size == maxSize;
		}
		
		public function get length():int
		{
			return size;
		}
		
		/**
		 * 循环队列
		 * @param data - 需要排队的数据
		 * @return - 队列长度
		 */  
		public function push(data:Object):int {  
			
			if(isFull())  
				throw Error("队列溢出");
			size++;  
			this.data[rear]= data;  
			rear=(rear+1) % maxSize;
			return size;  
		}  
		
		public function pop():Object {  
			
			if(isEmpty())  
				throw Error("队列为空");
			var temp:Object = this.data[this.front];
			this.data[this.front] = null;
			this.size--;    
			front = (front + 1) % maxSize;  
			return temp;  
		}  
	}
}